//
//  QuanlityOtherBannderViewController.m
//  MyProject
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "QuanlityOtherBannderViewController.h"
#import "TopicTableViewCell.h"
#import "HandleData.h"
#import "Topic.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "QuanlityViewController.h"
@interface QuanlityOtherBannderViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *topicArr;
@end

@implementation QuanlityOtherBannderViewController


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"isWhite"]) {
        self.tableView.backgroundColor = [UIColor grayColor];
            self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
            }else
    {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.barTintColor = [UIColor redColor];
            self.navigationController.navigationBar.barTintColor = [UIColor redColor];
        
    }
    [self.tableView reloadData];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"精品";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[TopicTableViewCell class] forCellReuseIdentifier:@"bannerTopic"];

    //      下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    
    self.tableView.header = header;
    
    //     马上进入刷新
    [self.tableView.header beginRefreshing];

    //     上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self handleData];
    }];
    
    self.tableView.footer = footer;


    
}

- (void)handleData
{
    NSString *str = [NSString stringWithFormat:@"http://open4.bantangapp.com/topic/list?client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=1115876&oauth_token=08e623418ebf12fe438fb9eed5d176da&track_deviceid=864516020760583&track_device_info=SM701&channel_name=UMENG_CHANNEL&app_installtime=1438818737013&app_versions=4.1.3&os_versions=4.4.2&screensize=1080&v=6&oauth_token=08e623418ebf12fe438fb9eed5d176da&screensize=1080&page=0&pagesize=100&ids=%@", self.banner.extend];
    
    [HandleData handleDataWithUrl:str completion:^(id data) {
    
        self.topicArr = [NSMutableArray array];
        NSArray *topicArr= [[data objectForKey:@"data"] objectForKey:@"topic"];
        
        for (NSDictionary *dic in topicArr) {
            Topic *topic = [[Topic alloc] init];
            [topic setValuesForKeysWithDictionary:dic];
            [self.topicArr addObject:topic];
        }
        [self.tableView reloadData];
        
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
   
        
    }];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    return self.topicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bannerTopic"];
    cell.backgroundColor = [UIColor clearColor];
    Topic *topic = [self.topicArr objectAtIndex:indexPath.row];
    cell.title.text = topic.title;

    [cell.pic sd_setImageWithURL:[NSURL URLWithString:topic.pic] placeholderImage:[UIImage imageNamed:@"lovebeijing.png"]];
    cell.likes.text = topic.likes;
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.bounds.size.height / 4;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QuanlityViewController *quanlityVC = [[QuanlityViewController alloc] init];
    [self.navigationController pushViewController:quanlityVC animated:YES];
    quanlityVC.topic = [self.topicArr objectAtIndex:indexPath.row];
    Topic *pic = [self.topicArr objectAtIndex:indexPath.row];
    quanlityVC.ID = pic.ID;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
